package com.aishua.fire.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.JsonArrayResult;
import com.aishua.fire.common.JsonObjectResult;
import com.aishua.fire.common.ValidateUtil;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.common.exp.ValidateException;
import com.aishua.fire.entity.checkitem.AddCheckItemDetailInfo;
import com.aishua.fire.entity.checkitem.AddCheckItemInfo;
import com.aishua.fire.entity.checkitem.CheckItemInfo;
import com.aishua.fire.service.CheckItemService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "巡检管理-检查项目管理",tags = {"巡检管理-检查项目模块接口"})
@RestController
@Slf4j
public class CheckItemManagerController
{

	@Autowired
	private CheckItemService itemService;
	
	@Autowired
	private ValidateUtil validateUtil;
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
	@ApiOperation(value="巡检管理-新增检查项目")
	@PostMapping(value = {"/checkitem"} )
	public ResponseEntity<JsonObjectResult> add(@RequestBody AddCheckItemInfo info, BindingResult result) throws ValidateException
	{
		log.info("检查项目分类目录_新增分类目录信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		
		boolean flag = itemService.save(info);
		
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("检查项目名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
	@ApiOperation(value="巡检管理-检查项目新增检查明细")
	@PostMapping(value = {"/checkitem/detail"} )
	public ResponseEntity<JsonObjectResult> addCheckitemDetail(@RequestBody AddCheckItemDetailInfo info, BindingResult result) throws ValidateException
	{
		log.info("巡检管理-检查项目新增检查明细:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		
		itemService.addCheckitemDetail(info);
		/*boolean flag = 
		
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("名称重复");
		}*/
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
    }
	
	
	/**
	 * 巡检管理-查询检查项目列表
	 */
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "巡检管理-查询检查项目列表")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
	@GetMapping(value = { "/checkitem" })
	public ResponseEntity<JsonArrayResult> list(
			@RequestParam(name = "item_name") String item_name/*,@RequestParam(name = "item_num") String item_num,
			@RequestParam(name = "item_type") String item_type*/,
			@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize) 
	{
		log.info("巡检管理-查询检查项目列表  查询列表请求参数:{}", item_name);
		
		List<CheckItemInfo> catalogs = itemService.list(item_name/*,item_num, item_type*/,pageNumber, pageSize);
		int total = itemService.listTotal(item_name/*,item_num,item_type*/);
		log.info("查询总条数：{}", total);
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(catalogs, total);

		return ResponseEntity.ok(r);
	}
	
	/**
	 * 巡检管理-查询检查项目详情
	 */
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "巡检管理-查询检查项目详情")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
	@GetMapping(value = { "/checkitem/detail" })
	public ResponseEntity<JsonObjectResult> iteminfo(@RequestParam(name = "id") String id) 
	{
		log.info("巡检管理-查询检查项目详情  查询列表请求参数:{}", id);
		
		CheckItemInfo iteminfo = itemService.getItemInfo(id);
		log.info("查询详情：{}", iteminfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(iteminfo);

		return ResponseEntity.ok(r);
	}
	
	/*@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkManageritem)
	@ApiOperation(value="巡检管理-修改检查项目")
	@PutMapping(value = {"/checkitem"} )
	public ResponseEntity<JsonObjectResult> update(@RequestBody CheckItemInfo info, BindingResult result) throws ValidateException
	{
		log.info("检查项目分类目录_修改检查项目信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		itemService.update(info);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);

		return ResponseEntity.ok(r);
    }*/
	
	/**
     * 巡检管理-查询检查项目列表
     */
    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "巡检管理-巡检点管理-查询当前巡检点关联的检查项目列表")
    @ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
    @GetMapping(value = { "/point/checkitem" })
    public ResponseEntity<JsonArrayResult> checkitemlist(
            @RequestParam(name = "p_id") String p_id) 
    {
        log.info("巡检管理-查询检查项目列表  查询列表请求参数:{}", p_id);
        
        List<CheckItemInfo> catalogs = itemService.checkitemlist(p_id);
        log.info("查询总条数：{}", catalogs);
        JsonArrayResult r = JsonArrayResult.makeSuccessResult(catalogs, 0);

        return ResponseEntity.ok(r);
    }
	
}